<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layuimini-container">
    <input type="hidden" name="rule_type" value="{$rule_type}">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            {if condition="$rules_type == 1"}
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
            {/if}
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" >编辑</a>
        </script>

        <script type="text/html" id="look">
            <button data-method="offset" data-type="auto" class="layui-btn " id="lookstyle" lay-event="look">查看</button>

        </script>
    </div>
</div>

{include file="public/footer" /}
<script type="text/html" id="rulesTitle">
    {{#  if(d.type === 1){ }}
    <span>获奖规则</span>
    {{# }else if(d.type === 2){ }}
    <span>签到规则</span>
    {{# }else if(d.type === 3){ }}
    <span>推荐规则</span>
    {{# }else if(d.type === 4){ }}
    <span>积分规则</span>
    {{# }else if(d.type === 5){ }}
    <span>用户服务协议</span>
    {{# }else if(d.type === 6){ }}
    <span>玩法教程</span>
    {{# }else if(d.type === 7){ }}
    <span>玩家攻略</span>
    {{# }else if(d.type === 8){ }}
    <span>如何分享获得权益</span>
    {{# }else if(d.type === 9){ }}
    <span>隐私政策</span>
    {{#  } }}
</script>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var render_array ={
            elem: '#currentTableId',
            url: '/admin/apply/rules_index?rules_type={$rules_type}&type={$type}&active=ajax',
            method: 'post',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data, //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],

            cols: [[
                // {type: "checkbox",},
                {field: 'type', title: '规则名称',sort: false, templet: '#rulesTitle'},
                {field: 'id', title: '规则内容',toolbar: '#look'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: false,
            skin: 'line'
        }
        var rule_type = $("input[name='rule_type']").val()
        if(rule_type == 1){
            render_array.cols = [[
                // {type: "checkbox",},
                {field: 'type', title: '规则名称',sort: false, templet: '#rulesTitle'},
                {field: 'id', title: '规则内容',toolbar: '#look'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]];

        }else if(rule_type == 2){
            render_array.cols = [[
                // {type: "checkbox",},
                {field: 'type', title: '协议名称',sort: false, templet: '#rulesTitle'},
                {field: 'id', title: '协议内容',toolbar: '#look'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]];
        }else if(rule_type == 0){
            render_array.cols = [[
                // {type: "checkbox",},
                {field: 'type', title: '教程名称',sort: false, templet: '#rulesTitle'},
                {field: 'id', title: '教程内容',toolbar: '#look'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]];
        }

        table.render(
            render_array
        );

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/apply/rules_add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = obj.data.id;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: "/admin/apply/rules_write?id="+id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定要删除么?', function (index) {
                    //发异步删除数据
                    $.ajax({
                        url: '/admin/apply/apply_del',
                        type: 'post',
                        dataType: 'json',
                        data: {id: id, mark:'qtype'},
                        success: function (data) {
                            if (data.status) {
                                // $(obj).parents("tr").remove();
                                layer.msg('删除成功!', {
                                    icon: 1,
                                    time: 1000
                                });
                                obj.del();
                            }
                        },
                        error: function () {
                            layer.msg("删除失败");
                        }
                    });
                    layer.close(index);
                });
            } else  if (obj.event === 'look') {
                //触发事件  查看内容
                var index = layer.open({
                    title:'查看内容',
                    type: 1,
                    area: ['100%', '100%'],
                    fixed: false, //不固定
                    shade:0.5,
                    shadeClose:true,
                    content: data.content,
                });
            }
        });
    });

</script>


</body>
</html>